package com.sghd.fight.module.game.config;

import com.sghd.pojo.fight.game.ChessType;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import java.io.Serializable;
import java.util.Objects;

/**
 * @author frank
 * @date 2018-12-20 11:28
 * @desc 联合主键
 **/
@Embeddable
public class ElementPk implements Serializable, Comparable<ElementPk> {

    @Enumerated(EnumType.STRING)
    @Column(name = "chessType", length = 50)
    private ChessType chessType;
    private int level;

    public static ElementPk valueOf(ChessType type, int level) {
        ElementPk pk = new ElementPk();
        pk.chessType = type;
        pk.level = level;
        return pk;
    }

    @Override
    public int compareTo(ElementPk o) {
        int i = this.chessType.ordinal() - o.chessType.ordinal();
        if (i != 0) {
            return i;
        } else {
            return this.level - o.level;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ElementPk pk = (ElementPk) o;
        return level == pk.level &&
                chessType == pk.chessType;
    }

    @Override
    public int hashCode() {
        return Objects.hash(chessType, level);
    }
}
